package org.easy.byteasr.dto;


import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;

import java.io.File;


public class MP3ToWavConverter {
    /**
     * 将 MP3 文件转换为 WAV 文件
     *
     * @param sourcePath MP3 文件的路径
     * @param targetPath 转换后 WAV 文件的路径
     * @return 转换成功返回 true，失败返回 false
     */
    public static boolean convertMP3ToWav(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);
        // 设置音频属性
        AudioAttributes audioAttributes = new AudioAttributes();
        audioAttributes.setCodec("pcm_s16le");
        audioAttributes.setBitRate(128000);
        audioAttributes.setChannels(2);
        audioAttributes.setSamplingRate(44100);
        // 设置编码属性
        EncodingAttributes encodingAttributes = new EncodingAttributes();
        encodingAttributes.setOutputFormat("wav");
        encodingAttributes.setAudioAttributes(audioAttributes);
        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, encodingAttributes);
            return true;
        } catch (IllegalArgumentException | EncoderException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {

        String sourcePath = "C:\\Users\\Administrator\\Desktop\\test.mp3";
        String targetPath = "C:\\Users\\Administrator\\Desktop\\test.wav";
        boolean result = convertMP3ToWav(sourcePath, targetPath);
        if (result) {
            System.out.println("MP3 转换为 WAV 成功！");
        } else {
            System.out.println("MP3 转换为 WAV 失败！");
        }
    }
}
